package com.xyzh05.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.service.UserManager;
import com.xyzh05.service.VipServiceManager;
import com.xyzh05.util.PageBean;
import com.xyzh05.model.Role;
import com.xyzh05.model.User;
import com.xyzh05.webapp.action.BaseAction;

import java.util.List;

public class UserAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = -7514841478424315722L;
	
	private UserManager userManager;
	private VipServiceManager vipServiceManager;
    private List<User> userList;
    private User user;
    private Role role;
    private Long id;
    private Long level;
    private String query;
    private Integer resultCode;
    private PageBean pageBean;
    private String major;
    private String nickname;
    
	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    }

    public String list() {
    	pageBean = userManager.showEditors(pageBean);
        return SUCCESS;
    }

	public String delete() {
        return SUCCESS;
    }

    public String edit() {
        return SUCCESS;
    }

    public String save() throws Exception {
        return SUCCESS;
    }
    
    /**
     * 查询普通编辑
     * @return
     */
    public String doShowOrdinaryEditor(){    	
    	pageBean = userManager.queryOrdinaryEditor(pageBean,major,nickname);    	
    	return SUCCESS;
    }
    
    /**
     * 更改编辑权限
     * @return
     */
    public String changeEditorLevel(){    	
    	try {    		
    		Boolean bool = vipServiceManager.isEditorService(id);
    		if(bool){
    			user = userManager.get(id);
    	    	role = roleManager.get(level);
    	    	if(Role.ROLE_TYPE_EDITOR_IN_CHIEF == level){
    	    		user.setLevel(user.getLevel() | Role.ROLE_TYPE_EDITOR_IN_CHIEF);
    	    	}else if(Role.ROLE_TYPE_EDITOR == level){
    	    		user.setLevel(user.getLevel() & (~Role.ROLE_TYPE_EDITOR_IN_CHIEF));
    	    	}
    			user.getRoles().clear();
    			user.addRole(role);
    			userManager.saveOrUpdate(user);
    			resultCode = 1;
    		}else{    			
        		resultCode = 2;
    		}    		
		} catch (Exception e) {
			resultCode = 0;
		}
    	return SUCCESS;
    }
    
    /**
     * 更改留学顾问权限
     * @return
     */
    public String changeAdviserLevel(){    	
    	try {    		
    		Boolean bool = vipServiceManager.isAdviserService(id);
    		if(bool){
    			user = userManager.get(id);
    	    	if(Role.ROLE_TYPE_ADVISER == user.getMainRole().getId()){
    	    		user.setLevel(user.getLevel() & (~Role.ROLE_TYPE_ADVISER));
        	    	if((user.getLevel() & Role.ROLE_TYPE_EDITOR_IN_CHIEF) != 0){
            	    	role = roleManager.get(Role.ROLE_TYPE_EDITOR_IN_CHIEF);
        	    	}else if((user.getLevel() & Role.ROLE_TYPE_ADMIN) != 0){
            	    	role = roleManager.get(Role.ROLE_TYPE_ADMIN);
        	    	}else if((user.getLevel() & Role.ROLE_TYPE_EDITOR) != 0){
            	    	role = roleManager.get(Role.ROLE_TYPE_EDITOR);
        	    	}else if((user.getLevel() & Role.ROLE_TYPE_USER) != 0){
            	    	role = roleManager.get(Role.ROLE_TYPE_USER);
        	    	}
    	    	}else {
    	    		user.setLevel(user.getLevel() | Role.ROLE_TYPE_ADVISER);
        	    	role = roleManager.get(Role.ROLE_TYPE_ADVISER);
    	    	}
    			user.getRoles().clear();
    			user.addRole(role);
    			userManager.saveOrUpdate(user);
    			resultCode = 1;
    		}else{    			
        		resultCode = 2;
    		}    		
		} catch (Exception e) {
			resultCode = 0;
		}
    	return SUCCESS;
    }
    
    
    public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getLevel() {
		return level;
	}

	public void setLevel(Long level) {
		this.level = level;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public void setVipServiceManager(VipServiceManager vipServiceManager) {
		this.vipServiceManager = vipServiceManager;
	}

	public VipServiceManager getVipServiceManager() {
		return vipServiceManager;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	
}